package com.zhj.springbootorderrabbitmqproducer.service;

import com.zhj.springbootorderrabbitmqproducer.pojo.Order;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author zhj
 */
@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void createByFanout(Order order) {
        // 查询库存
        System.out.println("库存充足···");
        // 扣减库存
        System.out.println("库存扣减成功！");
        // 创建订单
        order.setId(UUID.randomUUID().toString());
        System.out.println("订单生成成功！订单编号为： " + order.getId());
        // 消息发送
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,order.getId());
    }

    public void createByTopic(Order order) {
        // 查询库存
        System.out.println("库存充足···");
        // 扣减库存
        System.out.println("库存扣减成功！");
        // 创建订单
        order.setId(UUID.randomUUID().toString());
        System.out.println("订单生成成功！订单编号为： " + order.getId());
        // 消息发送
        String exchangeName = "topic_order_exchange";
        String routingKey = "com.sms.email";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,order.getId());
    }

    public void createByTTL(Order order) {
        // 查询库存
        System.out.println("库存充足···");
        // 扣减库存
        System.out.println("库存扣减成功！");
        // 创建订单
        order.setId(UUID.randomUUID().toString());
        System.out.println("订单生成成功！订单编号为： " + order.getId());
        // 消息发送
        String exchangeName = "ttl_direct_exchange";
        String routingKey = "ttl";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,order.getId());
    }

    public void createByTTLMessage(Order order) {
        // 查询库存
        System.out.println("库存充足···");
        // 扣减库存
        System.out.println("库存扣减成功！");
        // 创建订单
        order.setId(UUID.randomUUID().toString());
        System.out.println("订单生成成功！订单编号为： " + order.getId());
        // 消息发送
        String exchangeName = "ttl_direct_exchange";
        String routingKey = "ttl-message";
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("8000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName,routingKey,order.getId(),messagePostProcessor);
    }

    public void remove(Order order) {
        System.out.println("取消订单");
        order.setId(UUID.randomUUID().toString());
        String exchangeName = "direct_order_exchange";
        String routingKey = "sms.direct.key";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,order.getId());
    }

}
